import React, { Component } from 'react';
import moment from 'moment';
import { shallowEqualImmutable } from 'react-immutable-render-mixin';
import { NavigationActions, StackActions } from 'react-navigation';
import { DeviceEventEmitter } from 'react-native';
import { Loading } from '../wedgets';
import DeviceInfo from 'react-native-device-info';
import Toast from 'react-native-root-toast';
import { Utils, Fetch, Storage } from '../lib';

const systemName = DeviceInfo.getSystemName() || '';
const version = DeviceInfo.getSystemVersion() || '';
const brand = DeviceInfo.getBrand() || '';
const model = DeviceInfo.getModel() || '';

export const baseParams = {
  shop_id: '',
  gps: '0,0',
  _version: 'client|2.0.0',
  _device: 'einstein',
  _v: '1.0.0',
  _ua: `${systemName}|${version}|${brand}|${model}`,
  timestamp: moment().unix()
};
export default class BaseComponent extends Component {
  constructor(props) {
    super(props);

    // this.state = {
    //   group_type: 'scan_pay'
    // }
  }

  async componentDidMount() {
    // const group_type = await Storage.getData('group_type');

    // if (group_type)
    //   this.setState({
    //     group_type
    //   })
    //退出登录
    this.registerEvent('unlogin', () => {
      this.resetRoute('Login', 0);
    });
  }

  getGroupType = async () => {
    return (await Storage.getData('group_type')) || 'scan_pay';
  };

  shouldComponentUpdate(nextProps, nextState) {
    return (
      !shallowEqualImmutable(this.props, nextProps) ||
      !shallowEqualImmutable(this.state, nextState)
    );
  }
  //
  groupTypeCallBack = func => {
    Storage.getData('group_type').then(group_type => {
      func && func(group_type);
    });
  };
  //跳转页面
  goToComponent = (routeName, params) => {
    this.props.navigation.navigate(routeName, params);
  };
  //重置路由
  resetRoute = (stackName, index) => {
    // console.log('........',stackName,index)
    let resetAction = StackActions.reset({
      index: index || 0,
      actions: [NavigationActions.navigate({ routeName: stackName || 'Login' })]
    });

    this.props.navigation.dispatch(resetAction);
  };
  //返回上一页
  goBack = () => {
    this.props.navigation.goBack();
  };
  //发送订阅
  dispatchEvent = (target, params) => {
    DeviceEventEmitter.emit(target, params);
  };
  //接受订阅
  registerEvent = (target, eventFunc) => {
    this.emitter = DeviceEventEmitter.addListener(target, eventFunc);
  };
  //移除订阅
  componentWillUnmount() {
    this.emitter && this.emitter.remove();
  }
  //弹出loading框
  Loading = ({ title = '', isOverlay } = {}) => {
    Loading.show({
      title, //标题
      isOverlay //是否有遮罩
    });
  };
  //隐藏loading框
  UnLoading = key => {
    Loading.hide();
  };
  // 弹出安卓提示框
  Toast = message => {
    Toast.show(message);
  };
  //组装请求参数Utils.object2query(this.bodys)
  initBaseParams = (url, params) => {
    return url + Utils.object2query(Object.assign(baseParams, params));
  };
  async fetch({ url = '', method = 'get', data = {}, params = {} }) {
    const uuid = (await Storage.getData('uuid')) || '';

    Loading.show({
      title: '加载中...', //标题
      isOverlay: true //是否有遮罩
    });

    return new Promise((resolve, reject) => {
      new Fetch({
        url,
        method,
        headers: { 'mx-uuid': uuid },
        data,
        params: {
          ...baseParams,
          ...params
        }
      })
        .dofetch()
        .then(res => {
          Loading.hide();

          resolve(res);
        })
        // .catch(err => {
        //   Loading.hide();

        //   resolve({
        //     status: 'error',
        //     result: {
        //       error_msg: '服务器开小差了~'
        //     }
        //   });
        // });
    });
  }
}
